package com.mxf.agricultural.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.mxf.agricultural.common.ErrorCode;
import com.mxf.agricultural.constant.CommonConstant;
import com.mxf.agricultural.exception.BusinessException;
import com.mxf.agricultural.exception.ThrowUtils;
import com.mxf.agricultural.mapper.OrderMapper;
import com.mxf.agricultural.model.dto.order.OrderQueryRequest;
import com.mxf.agricultural.model.entity.*;
import com.mxf.agricultural.service.OrderService;
import com.mxf.agricultural.service.UserService;
import com.mxf.agricultural.utils.SqlUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author JD
 * @description 针对表【order(商品)】的数据库操作Service实现
 * @createDate 2024-02-11 19:11:26
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order>
        implements OrderService {

    @Resource
    private UserService userService;

    @Override
    public void validOrder(Order order, boolean add) {
        if (order == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String title = order.getName();
        // 创建时，参数不能为空
        if (add) {
            ThrowUtils.throwIf(StringUtils.isAnyBlank(title), ErrorCode.PARAMS_ERROR);
        }
    }

    /**
     * 获取查询包装类
     *
     * @param orderQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<Order> getQueryWrapper(OrderQueryRequest orderQueryRequest) {
        Long num = orderQueryRequest.getNum();
        String name = orderQueryRequest.getName();
        Long payeeid = orderQueryRequest.getPayeeid();
        Long payerid = orderQueryRequest.getPayerid();
        Long price = orderQueryRequest.getPrice();

        int current = orderQueryRequest.getCurrent();
        int pageSize = orderQueryRequest.getPageSize();
        String sortField = orderQueryRequest.getSortField();
        String sortOrder = orderQueryRequest.getSortOrder();

        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();

        // 拼接查询条件
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);
        queryWrapper.eq(ObjectUtils.isNotEmpty(num), "num", num);
        queryWrapper.eq(ObjectUtils.isNotEmpty(payeeid), "payeeid", payeeid);
        queryWrapper.eq(ObjectUtils.isNotEmpty(payerid), "payerid", payerid);
        queryWrapper.eq(ObjectUtils.isNotEmpty(price), "price", price);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }



    @Override
    public Page<Order> getOrderPage(Page<Order> orderPage, HttpServletRequest request) {
        List<Order> orderList = orderPage.getRecords();
        Page<Order> orderVOPage = new Page<>(orderPage.getCurrent(), orderPage.getSize(), orderPage.getTotal());
        if (CollUtil.isEmpty(orderList)) {
            return orderVOPage;
        }
        // 1. 关联查询用户信息
        Set<Long> userIdSet = orderList.stream().map(Order::getPayerid).collect(Collectors.toSet());
        Map<Long, List<User>> userIdUserListMap = userService.listByIds(userIdSet).stream()
                .collect(Collectors.groupingBy(User::getId));

        return orderVOPage;
    }
}




